﻿import mx.rpc.http.HTTPService;
import mx.rpc.events.ResultEvent;
import mx.managers.PopUpManager;
import mx.collections. *;
import mx.controls. *;
import com.esri.ags.Map;
import com.esri.ags.geometry.Extent;
import com.uprr.app.tnm.model.Model;
import com.uprr.app.tnm.events.AppEvent;
import com.esri.ags.Graphic;
import com.esri.ags.symbol. *;
import com.esri.ags.layers. *;
import com.esri.ags.tasks.AddressCandidate;
import mx.rpc.AsyncResponder;
import com.adobe.serialization.json.JSON

// PictureMarker - embedded image
 [Embed (source = '/assets/i_pushpin.png')]
private var picEmbeddedClass : Class;
private var pinMarker : PictureMarkerSymbol = new PictureMarkerSymbol (picEmbeddedClass);

private function handleCreationComplete () : void
{
	// Center the TitleWindow container
	// over the control that created it.
	PopUpManager.centerPopUp (this);
	this.showCloseButton = true;
}
// when something goes wrong for all HTTP services
private function serviceFaultHandler (event : Event) : void
{
	var errorEvent : AppEvent = new AppEvent (AppEvent.APP_ERROR);
	errorEvent.data = "Error when processing geocoding query";
	errorEvent.dispatch ();
}

private function doFind () : void
{
	var myAddress : Object = {
		Address : address.text,
		City : city.text,
		State : state.text,
		Zip : zip.text,
		Country : country.text
	};
	// Use outFields to get back extra information
	// The exact fields available depends on the specific Locator used.
	var myOutFields : Array = ["Loc_name"];
	locateTask.addressToLocations (myAddress, myOutFields, new AsyncResponder (onResult, onFault));
}
	/**
	*
	*/
private	function onResult (candidates : Array, token : Object = null) : void
	{
	 
		var myMap : Map = Model.getInstance ().map;
		var found:Boolean=false;
		
		if (candidates.length > 0)
		{
			 
			found=true;
			var addressCandidate : AddressCandidate = candidates [0];
			var myGraphic : Graphic = new Graphic ();
			myGraphic.geometry = addressCandidate.location;
			myGraphic.symbol = pinMarker;
			myGraphic.toolTip = addressCandidate.address.toString ();
			myGraphic.id = "graphic";
			Model.getInstance ().graphicsLayer.add (myGraphic);
			myMap.centerAt (addressCandidate.location);
			
			// if found, close the win   why this is not working ?
			//PopUpManager.removePopUp (this);
			
			// Zoom to an appropriate level
			// Note: your tile levels and LOC_NAME might differ...
			if (addressCandidate.attributes.Loc_name.search ("RoofTop") > 0)
			{
				myMap.scale = 10000;
			} else if (addressCandidate.attributes.Loc_name.search ("Address") > 0)
			{
				myMap.scale = 10000;
			} 
			else if (addressCandidate.attributes.Loc_name.search ("Street") > 0)
			{
				myMap.scale = 15000;
				
			} 
			else if (addressCandidate.attributes.Loc_name.search ("Zipcode") > 0)
			{
				myMap.scale = 40000;
				
			} 
			else if (addressCandidate.attributes.Loc_name.search ("City") > 0)
			{
				myMap.scale = 150000;
			} 
			else 
			{
				myMap.scale = 500000;
			}
		 

			//myInfo.htmlText = "<b>Found:</b><br/>" + addressCandidate.address.toString();  // formated address
			
		} else 
		{
			// does no thing, if not
			//myInfo.htmlText = "<b><font>No location was found:(</b></font>";
			//Alert.show("Sorry, couldn't find a location for this address"  + "\nAddress: " + address.text  + "\nCity: " + city.text + "\nZIP Code: "+ zip.text+ "\nState: " + state.text+ "\nCountry: " + country.text);
			
		}
		
			if (found){
				PopUpManager.removePopUp (this);
			
			}
 
	}
	
	
	
private	function onFault (info : Object, token : Object = null) : void 
	{
		var errorEvent : AppEvent = new AppEvent (AppEvent.APP_ERROR);
		errorEvent.data = "Error when processing geocoding query";
		errorEvent.dispatch ();
	}

